package com.ruoyi.system.controller;
import java.util.HashMap;
import com.ruoyi.supplier.domain.TOrders;
import com.ruoyi.supplier.service.ITOrdersService;
import com.ruoyi.system.domain.TGoods;
import com.ruoyi.system.domain.TGoodsInorders;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TGoodsInorders;
import com.ruoyi.system.mapper.TOrderDetailMapper;

import com.ruoyi.system.domain.VO.TOrderDetailVO;
import com.ruoyi.system.service.ITGoodsInordersService;
import com.ruoyi.system.service.ITGoodsService;
import com.ruoyi.system.service.ITPurchaserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TOrderDetail;
import com.ruoyi.system.service.ITOrderDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单信息Controller
 * 
 * @author ruoyi
 * @date 2024-05-29
 */
@RestController
@RequestMapping("/buyers/orderDetailInfo")
public class TOrderDetailController extends BaseController
{
    @Autowired
    private ITOrderDetailService tOrderDetailService;

    @Autowired
    private ITOrdersService tOrdersService;

    @Autowired
    private ITGoodsService tGoodsService;

    @Autowired
    private ITGoodsInordersService tGoodsInordersService;


    @Autowired
    private ITPurchaserService tPurchaserService;

    /**
     * 查询订单信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TOrderDetail tOrderDetail)
    {
        tOrderDetail.setPurchaseId(SecurityUtils.getUserId());
        startPage();
        List<TOrderDetail> list = tOrderDetailService.selectTOrderDetailList(tOrderDetail);
        return getDataTable(list);
    }

    /**
     * 查询订单信息列表
     */
    @GetMapping("/listVO/{orderId}")
    public TableDataInfo listVO(@PathVariable("orderId")String orderId)
    {
        startPage();
        TOrderDetail tOrderDetail = tOrderDetailService.selectTOrderDetailByOrderId(orderId);
        List<TGoodsInorders> tGoodsInordersList = tOrderDetail.getTGoodsInordersList();
        List<TOrderDetailVO> result = new ArrayList<>();
        for (TGoodsInorders tGoodsInorders : tGoodsInordersList) {
            String goodsId = tGoodsInorders.getGoodsId();
            Long amount = tGoodsInorders.getAmount();
            TGoods tGoods = tGoodsService.selectTGoodsByGoodsId(goodsId);
            String goodsId_ = tGoods.getGoodsId();
            String goodsName = tGoods.getGoodsName();
            Long price = tGoods.getPrice();
            String goodsPicture = tGoods.getGoodsPicture();
            TOrderDetailVO tOrderDetailVO = new TOrderDetailVO();
            tOrderDetailVO.setGoodsId(goodsId_);
            tOrderDetailVO.setGoodsName(goodsName);
            tOrderDetailVO.setPrice(price);
            tOrderDetailVO.setGoodsPicture(goodsPicture);
            tOrderDetailVO.setAmount(amount);
            result.add(tOrderDetailVO);
        }
        return getDataTable(result);
    }

    /**
     * 导出订单信息列表
     */
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TOrderDetail tOrderDetail)
    {
        tOrderDetail.setPurchaseId(SecurityUtils.getUserId());
        List<TOrderDetail> list = tOrderDetailService.selectTOrderDetailList(tOrderDetail);
        List<TGoodsInorders> li=new ArrayList<>();
        TGoodsInorders li2=new  TGoodsInorders();
        for (TOrderDetail tOrderDetail1 : list) {
            li2.setOrderId(tOrderDetail1.getOrderId());
            tOrderDetail1.setTGoodsInordersList(tGoodsInordersService.selectTGoodsInordersList(li2));
        }
        ExcelUtil<TOrderDetail> util = new ExcelUtil<TOrderDetail>(TOrderDetail.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return success(tOrderDetailService.selectTOrderDetailByOrderId(orderId));
    }

    /**
     * 新增订单信息
     */
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TOrderDetail tOrderDetail)
    {
        tOrderDetail.setPurchaseId(SecurityUtils.getUserId());
        tOrderDetail.setStatus("2");
        tOrderDetail.setUpdateBy(SecurityUtils.getUserId().toString());
        if(tOrderDetail.getAddr()==null||tOrderDetail.getAddr().equals(""))
            tOrderDetail.setAddr(tPurchaserService.selectTPurchaserByPurchaserId(tOrderDetail.getPurchaseId()).getAddr());
        return success(tOrderDetailService.insertTOrderDetail(tOrderDetail));
    }

    /**
     * 修改订单信息
     */
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOrderDetail tOrderDetail)
    {
        return toAjax(tOrderDetailService.updateTOrderDetail(tOrderDetail));
    }

    /**
     * 删除订单信息
     */
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds)
    {
        return toAjax(tOrderDetailService.deleteTOrderDetailByOrderIds(orderIds));
    }

    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping("/addInfo/{orderId}")
    public AjaxResult addIndo(@RequestBody TGoodsInorders[]  tGoodsInorders,@PathVariable String orderId)
    {
        List<TGoodsInorders> list = new ArrayList<TGoodsInorders>();
        for (TGoodsInorders tGoodsInorder : tGoodsInorders)
        {
            tGoodsInorder.setOrderId(orderId);
            list.add(tGoodsInorder);
        }
        return toAjax(tOrderDetailService.insertTOrderInfo(list));
    }
}
